package com.atguigu.wc.test;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.AggregateOperator;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

/**
 * @Author:GaoFei
 * @Description:
 * @Date:Created in 18:11
 * @Modified By:
 */
public class WordCountOffLine {
    public static void main(String[] args) throws Exception {
        ExecutionEnvironment environment = ExecutionEnvironment.getExecutionEnvironment();
        String inputPath = "D:\\1BIGDATA\\spark-streaming\\Flink\\src\\main\\resources\\hello.txt";
        DataSet<String> inputDataSource = environment.readTextFile(inputPath);

        DataSet<Tuple2<String, Integer>> sum = inputDataSource
                .flatMap(new MyFla())
                .groupBy(0)
                .sum(1);
            sum.print();

    }
    public static class MyFla implements FlatMapFunction<String,Tuple2<String,Integer>> {
        @Override
        public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
            String[] words = value.split(" ");
            for (String word : words) {
                out.collect(new Tuple2<>(word,1));
            }
        }
    }
}
